<script src="../../utils/auth.js"></script>
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div id="search1">
        <label class="el-form-item-label">变电站</label>
        <el-select v-model="query.substationName" placeholder="请选择" @change="getRunNumbers">
          <el-option
            v-for="item in substationSearchOption"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </div>

      <div v-if="crud.props.searchToggle" id="search2">
        <!-- 搜索 -->
        <label class="el-form-item-label">变压器运行编号</label>
        <el-select v-model="query.runNumber" placeholder="变压器运行编号" @change="crud.toQuery()">
          <el-option
            v-for="item in runNumberSearchOption"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <!--        <el-input v-model="query.runNumber" clearable placeholder="变压器运行编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />-->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="设备型号">
            <el-input v-model="form.equipmentModel" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="变电站名称">
            <el-input v-model="form.substationName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="生产厂家">
            <el-input v-model="form.manufacturer" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="生产日期">
            <el-date-picker v-model="form.dateOfManufacture" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="投运日期">
            <el-date-picker v-model="form.dateOfPut" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="电压等级">
            <el-input v-model="form.voltageClasses" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="变压器运行编号">
            <el-input v-model="form.runNumber" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="equipmentModel" label="设备型号" />
        <el-table-column prop="substationName" label="变电站名称" />
        <el-table-column prop="manufacturer" label="生产厂家" />
        <el-table-column prop="dateOfManufacture" label="生产日期" />
        <el-table-column prop="dateOfPut" label="投运日期" />
        <el-table-column prop="voltageClasses" label="电压等级" />
        <el-table-column prop="runNumber" label="变压器运行编号" />
        <el-table-column
          v-if="checkPer(['admin','transformerBasic:edit','transformerBasic:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTransformerBasic from '@/api/transformerBasic'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUDNew'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getToken} from '@/utils/auth'

const defaultForm = {
  id: null,
  createTime: null,
  updateTime: null,
  createBy: null,
  updateBy: null,
  equipmentModel: null,
  substationName: null,
  manufacturer: null,
  dateOfManufacture: null,
  dateOfPut: null,
  voltageClasses: null,
  runNumber: null
}
export default {
  name: 'TransformerBasic',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '变压器基本信息',
      url: 'api/transformerBasic',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudTransformerBasic}
    })
  },
  data() {
    return {
      substationSearchOption: [],
      runNumberSearchOption: [],
      substationSearch: '',
      permission: {
        add: ['admin', 'transformerBasic:add'],
        edit: ['admin', 'transformerBasic:edit'],
        del: ['admin', 'transformerBasic:del']
      },
      rules: {},
      queryTypeOptions: [
        {key: 'runNumber', display_name: '变压器运行编号'}
      ]
    }
  },
  created() {
    this.getSubstations();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getSubstations() {
      var axios = require('axios');
      const self = this
      var config = {
        method: 'get',
        url: 'http://localhost:8013/api/transformerBasic/querySubstation',
        headers: {
          'Authorization': getToken()
        }
      };

      axios(config)
        .then(function (response) {
          self.substationSearchOption = response.data;
          console.log(JSON.stringify(response.data));
        })
        .catch(function (error) {
          console.log(error);
        });

    },
    getRunNumbers() {
      var axios = require('axios');
      const self = this
      var config = {
        method: 'get',
        url: 'http://localhost:8013/api/transformerBasic/queryRunNumbersBySubstation?substationName=' + self.crud.query.substationName,
        headers: {
          'Authorization': getToken()
        }
      };

      axios(config)
        .then(function (response) {
          self.runNumberSearchOption = response.data;
          console.log(JSON.stringify(response.data));
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  }
}
</script>

<style scoped>
#search1 {
  position: relative;
  left: 1%;
  display: inline-block;
}

#search2 {
  position: relative;
  left: 2%;
  display: inline-block;
}
</style>
